//index.js
//获取应用实例
const app = getApp()
let Axios = require('../../../common/js/ajax.js'); //引入Axios方法
let Compress = require('../../../common/js/compressImage.js'); //引入Axios方法
let WxValidate = require('../../../common/js/WxValidate.js'); //表单验证
let Mtils = require('../../../common/js/Mtils.min.js');
Mtils = Mtils.Mtils
Page({
	data: {
		openid: '',
		nickName: '',
		avatarUrl: '',
		curentNav: 2, //当前导航
		compress:true,
		selfs: false,
		canvasWidth: 0,
		canvasHeight: 0,
		pickerValueDefault: '',
		titles: '',
		info: {}
	},
	onLoad: function() {
		if (app.globalData.nickName && app.globalData.avatarUrl) {
			this.setData({
				nickName: app.globalData.nickName,
				avatarUrl: app.globalData.avatarUrl
			})
		}
		var _this = this;
		this.initValidate()
		this.uid = app.globalData.uid || ''
		this.Md5 = Mtils.security.hex_md5(this.uid) + app.globalData.secret;
		this.Md5 = Mtils.security.hex_md5(this.Md5);
		this.timesp = Mtils.now();
		Axios.get(app.globalData.baseUrl + 'App/Store/Store/index', {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5,
		}, (res) => {
			console.log(res)
			if (res.status == 1) {
				var data = res.info
				_this.setData({
					info: data,
					pickerValueDefault: data.address_path
				})
				_this.picker = this.selectComponent("#city");
				_this.picker.int_address();
				_this.picker.init();
			} else {
				_this.showModal(res)
			}
		})
	},
	onReady: function() {
		wx.setNavigationBarTitle({
			title: '申请店铺'
		})
	},
	showPicker: function() {
		this.picker.showPicker()
	},
	_getLabel: function(e) {
		console.log(e)
		this.setData({
			pickerValueDefault: e.detail.path,
			titles: e.detail.name
		})
	},
	handleType: function(e) {
		var index = e.currentTarget.dataset.id
		this.setData({
			'info.subject_property': index
		})
	},
	handleSelfTap: function() {
		var type = this.data.info.is_corporation_my
		if (type == 0) {
			this.setData({
				'info.is_corporation_my': 1
			})
		} else {
			this.setData({
				'info.is_corporation_my': 0
			})
		}
	},
	chooseImage: function(e) {
		var _this = this
		var index = e.currentTarget.dataset.type
		var type = ['album', 'camera'];	
		this.setData({
			compress:true
		})
		wx.chooseImage({
			sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片
			sourceType: type, //可选择性开放访问相册、相机
			success: function(res) {
				wx.showLoading({
					title: '正在上传中',
				})
				Compress.compress(res.tempFilePaths[0], _this, function(res) {
					wx.uploadFile({
						url: app.globalData.baseUrl + 'App/PublicCommon/Public/UploadPicture',
						filePath: res,
						name: 'file',
						formData: {
							uid: _this.uid,
							sign: _this.Md5,
							time: _this.timesp
						},
						success: function(res) {
							wx.hideLoading();
							_this.setData({
								compress:false
							})
							var data = JSON.parse(res.data) //坑2：与wx.request不同，wx.uploadFile返回的是[字符串]，需要自己转为JSON格式
							console.log(data)
							var info = data.info
							if (data.status == 1) {
								if (index == 0) {
									_this.setData({
										'info.corporation_id_card_front': info.save_path
									})
								} else if (index == 1) {
									_this.setData({
										'info.business_license': info.save_path
									})
								} else if (index == 2) {
									_this.setData({
										'info.qualification_certificate': info.save_path
									})
								} else {
									_this.setData({
										'info.icon': info.save_path
									})
								}
							} else {
								wx.hideLoading();
								_this.showModal(res)
							}
						}
					})
				})
			}
		})
	},
	deleteFunc: function(e) {
		var _this = this
		var index = e.currentTarget.dataset.id
		if (index == 0) {
			_this.setData({
				'info.corporation_id_card_front': ''
			})
		} else if (index == 1) {
			_this.setData({
				'info.business_license': ''
			})
		} else if (index == 2) {
			_this.setData({
				'info.qualification_certificate': ''
			})
		}
	},
	submit: function(e) {
		console.log(e.detail)
		var params = e.detail.value
		if (!this.WxValidate.checkForm(params)) {
			const error = this.WxValidate.errorList[0]
			this.showModal(error)
			return false
		}
		this.submitInfo(params);
	},
	submitInfo: function(e) {
		var CONST = {
			uid: this.uid,
			time: this.timesp,
			sign: this.Md5
		}
		var obj = Object.assign(e, app.globalData.temp, CONST);
		Axios.post(app.globalData.baseUrl + 'App/Store/Store/index',
			obj, (res) => {
				console.log(res)
				if (res.status == 1) {
					var data = res.info
					wx.navigateTo({
						url: '/pages/applyShop/success/success'
					})
				} else {
					// this.showModal(res, '/pages/center/center')
				}
			})
	},
	showModal: function(error, toUrl) {
		wx.showModal({
			content: error.msg,
			showCancel: false,
			success: function(res) {
				if (res.confirm) {
					//点击确定以后执行方法或者跳转
					if (toUrl) {
						wx.navigateTo({
							url: toUrl
						})
					}
				}
			}
		})
	},
	initValidate: function() {
		/** 4-2(配置规则)*/
		const rules = {
			subject_property: {
				required: true,
			},
			address_path: {
				required: true,
			},
			subject_name: {
				required: true,
			},
			subject_operate: {
				required: true,
			},
			// 配置false可关闭验证
			subject_detail: {
				required: true,
				rangelength: [2, 200]
			},
			is_corporation_my: {
				required: true,
			},
			corporation_name: {
				required: true,
			},
			corporation_id_card: {
				required: true,
				idcard: true
			},
			corporation_phone: {
				required: true,
				tel: true
			},
			corporation_id_card_front: {
				required: true,
			},
			business_license: {
				required: true,
			},
			qualification_certificate: {
				required: true,
			},
			title: {
				required: true,
			},
			icon: {
				required: true,
			},
			description: {
				required: true,
				rangelength: [2, 80]
			},
		}
		// 验证字段的提示信息，若不传则调用默认的信息
		const messages = {
			subject_property: {
				required: '请选择主体性质',
			},
			address_path: {
				required: '选择店铺地址',
			},
			subject_name: {
				required: '请输入主体全称',
			},
			subject_operate: {
				required: '请输入主营信息',
			},
			// 配置false可关闭验证
			subject_detail: {
				required: '请输入详情介绍',
				rangelength: '最多200字以内'
			},
			is_corporation_my: {
				required: '',
			},
			corporation_name: {
				required: '请输入法人姓名',
			},
			corporation_id_card: {
				required: '请您输入身份证号',
				idcard: '请填写正确的身份证号'
			},
			corporation_phone: {
				required: '请您输入手机号',
				tel: '请填写正确的手机号'
			},
			corporation_id_card_front: {
				required: '请上传身份证正面',
			},
			business_license: {
				required: '请上传营业执照',
			},
			qualification_certificate: {
				required: '请上传资质证书',
			},
			title: {
				required: '请输入店铺名',
			},
			icon: {
				required: '请上传头像',
			},
			description: {
				required: '请选择主体性质',
				rangelength: '最多输入80字'
			},
		}
		// 创建实例对象
		this.WxValidate = new WxValidate.WxValidate(rules, messages)
		/**
		 * 也可以自定义验证规则
		 */
		// 		this.WxValidate.addMethod('assistance', (value, param) => {
		// 			return this.WxValidate.optional(value) || (value.length >= 1 && value.length <= 2)
		// 		}, '请勾选 《顺风男服务协议》')
	}
})
